
#!/bin/bash
#准备阶段


#创建目录存放程序
if [ -f ./tmp ]
then
rm ./tmp
fi

if [ ! -d ./envs ]
then
  mkdir envs
  mkdir -p ./envs/nodelibs
fi
ldd ~/ros2_ws/install/mavros/lib/mavros/mavros_node | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/fcu_comm/lib/fcu_comm/fcu_comm_node | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/fcu_comm/lib/fcu_comm/follow_attack_node | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/fcu_comm/lib/fcu_comm/vision_locate_node | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/dds2ros/DDS2ROS | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/behavior-process/lib/behavior-process/behavior-node | awk '{print $3}' >> ./tmp
#ldd ~/ros2_ws/install/usb_cam_pkg/lib/usb_cam_pkg/usb_cam_node | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/video_distribution/lib/video_distribution/video_distribution | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/new_cluster/lib/new_cluster/Cluster_formation_control_node_new | awk '{print $3}' >> ./tmp
#ldd ~/ros2_ws/install/xe_thru/lib/xe_thru/x4m300 | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/q10f_pkg/lib/q10f_pkg/q10f_node | awk '{print $3}' >> ./tmp


if [ -f ./tmp ]
then
while read line
do
  ros=$(echo $line | grep "/opt/ros")
  custom=$(echo $line | grep "libcustom_msgs")
  
  if [  "$ros" == ""  -a  "$custom" == "" ] 
  then

    filepath="$line"
    #items=(${line////})
    len=${#filepath}
 
    #cp $line ./test/${items[$len-1]}
    if [ ! $len -eq 0 ]
    then
    
      filename=$(basename $line)

      if [  -f ./envs/nodelibs/${filename} ]
      then
        continue
      fi
      echo "copy file $line"
      cp -n $line ./envs/nodelibs/
      status=$?
      if [ ! $status -eq 0 ]
      then
        echo "cp file $line failed"
      fi

    fi

  fi
done < ./tmp

rm ./tmp
fi

cd envs
#pack yolo
#depend module
if [ -d ~/yolo-depend ]
then
echo "copy yolodepend from ~/yolo-depend"
cp ~/yolo-depend/* ./
else
echo "no yolo depend path ~/yolo-depend"
fi
#run_model
if [ -f ~/run_model.zip ]
then
echo "copy run_model.zip from ~/"
cp ~/run_model.zip ./
elif [ -d ~/run_model ]
then
echo "zip -r ./run_model.zip ~/run_model"
zip -r ./run_model.zip ~/run_model
else
echo "no run_model for yolo"
fi

#pack ros

tar -czvf foxy.tar.gz /opt/ros/foxy

cd ..

tar -czvf envs.tar.gz envs
cat distribution-env-install.sh envs.tar.gz > distribution-env-install.run

chmod +x distribution-env-install.run

#rm -fr envs
#rm -fr envs.tar.gz
echo "打包完成"





